//
//  YSProductListGridItemCell.m
//  Yueshi
//
//  Created by Northwan on 15/7/21.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//

#import "YSProductListGridItemCell.h"

#define PromotionBtnHeight 25

@interface YSProductListGridItemCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *discountLabel;
@property (strong, nonatomic) UIButton *promotionBtn;
@end

@implementation YSProductListGridItemCell

-(void)awakeFromNib{
    if(!self.promotionBtn){
        UIButton *promotionBtn = [[UIButton alloc]init];
        [promotionBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [promotionBtn.titleLabel setFont:SYS_FONT(14)];
        [promotionBtn setBackgroundImage:[UIImage resizedImage:@"bg_list_promotion"] forState:UIControlStateNormal];
        [promotionBtn setFrame:CGRectMake(0, YSPublicXInset, 50,PromotionBtnHeight)];
        [promotionBtn setHidden:YES];
        self.promotionBtn = promotionBtn;
        [self.iconView addSubview:promotionBtn];
    }
}


+ (instancetype)cellWithCollectionView:(UICollectionView *)collectionView indexPath:(NSIndexPath *)indexPath{
    
    UINib *nib = [UINib nibWithNibName:@"YSProductListGridItemCell" bundle: [NSBundle mainBundle]];
    [collectionView registerNib:nib forCellWithReuseIdentifier:@"YSProductListGridItemCell"];
    YSProductListGridItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"YSProductListGridItemCell" forIndexPath:indexPath];
    return cell;
}

-(void)setProd:(YSProductList *)prod{
    [self.iconView setImageURLStr:prod.prodThumbnails];
    if(prod.prodPromotion){
        [self.promotionBtn setHidden:NO];
        if(prod.prodPromotion.isPanicBuy){
            NSString *promotionPrice = nil;
            if(prod.prodPromotion.whetherShipping){
                promotionPrice = [NSString stringWithFormat:@"限时包邮¥%.1f",prod.prodPromotion.price];
            }else{
                promotionPrice = [NSString stringWithFormat:@"限时抢购¥%.1f",prod.prodPromotion.price];
            }
            [self.promotionBtn setTitle:promotionPrice forState:UIControlStateNormal];
            CGSize promotionPriceSize =   [promotionPrice sizeWithFont:SYS_FONT(14)];
            [self.promotionBtn setWidth:promotionPriceSize.width+YSPublicXInset*2];
        }else if(prod.prodPromotion.isPresent){
            NSString *presentStr = @"买就赠";
            [self.promotionBtn setTitle:presentStr forState:UIControlStateNormal];
            CGSize presentSize =  [presentStr sizeWithFont:SYS_FONT(14)];
            [self.promotionBtn setWidth:presentSize.width+YSPublicXInset*2];
        }
    }else{
        [self.promotionBtn setHidden:YES];
    }
    [self.nameLabel setText:prod.prodShortName];
    [self.priceLabel setText:[NSString stringWithFormat:@"¥%.1f",prod.prodNewPrice]];
    [self.discountLabel setText:[NSString stringWithFormat:@"已售%ld",prod.prodBuyCount]];
}

@end